package com.yoouce.patchmng.controller;

import com.yoouce.patchmng.bean.PatchInfo;
import com.yoouce.patchmng.bean.TreeObject;
import com.yoouce.patchmng.bean.UserInfo;
import com.yoouce.patchmng.init.SystemContants;
import com.yoouce.patchmng.util.PropertiesUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询补丁历史记录
 * Created by administrator on 15-9-29.
 */
@Controller
public class HistoryController extends BaseController {

    @RequestMapping(value = "/queryHistory")
    public ModelAndView index() {


        //获取session
        UserInfo userInfo = (UserInfo) getSession().getAttribute(SystemContants.USER_KEY);

        Map<String, List<PatchInfo>> resultMap = new HashMap<String, List<PatchInfo>>();
        List<PatchInfo> patchInfos = new ArrayList<PatchInfo>();
        //获取所有补丁文件目录
        String patchPath = request.getSession().getServletContext().getRealPath(
                "/upload/patch");
        iteratorFiles(patchPath,patchInfos,userInfo);

        resultMap.put("patchInfos", patchInfos);
        ModelAndView mav = new ModelAndView("history", resultMap);
        return mav;
    }


    /**
     * 根据目录迭代查询目录下的所有Properties文件，并讲结果放入到patchInfos列表中
     *
     * @param path       路径地址
     * @param patchInfos 结果对象
     * @param userInfo   用户信息
     */
    private void iteratorFiles(String path, List<PatchInfo> patchInfos, UserInfo userInfo) {



        File files = new File(path);
        File[] tempList = files.listFiles();

        if (tempList != null) {
            for (File file : tempList) {
                PatchInfo patchInfo = new PatchInfo();

                //判断是否为文件，如果为文件则判断是否为Properties文件，如果是则读取内容
                if (file.isFile()) {
                    if (hasRole(userInfo, file.getName())) {
                        if (StringUtils.endsWith(file.getName(), ".properties")) {

                            patchInfo = PropertiesUtils.readPropertiesFile(file);
                            patchInfos.add(patchInfo);
                        }



                    }
                }

                //判断是否为文件夹，如果为文件夹，则继续迭代
                if(file.isDirectory())
                {
                    iteratorFiles(file.getPath(),patchInfos,userInfo);
                }

            }
        }
    }

    /**
     * 判断用户是否具有目录的权限
     *
     * @param userInfo 用户信息
     * @param fileName 文件目录
     * @return boolean
     */
    private boolean hasRole(UserInfo userInfo, String fileName) {
        boolean result = false;
        if (userInfo != null) {

            if ("admin".equals(userInfo.getUserName())) {
                result = true;
            } else {
                if (fileName.contains(userInfo.getUserName())) {
                    result = true;
                }
            }

        }
        return result;
    }

}
